home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTCREATE.C < prev    next >
Text File  |  1991-09-23  |  5KB  |  170 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btcreate.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19.  
  20. /* local headers */
  21. #include "btree_.h"
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      btcreate - create a btree
  26.  
  27. SYNOPSIS
  28.      int btcreate(filename, m, keysize, fldc, fldv)
  29.      const char *filename;
  30.      int m;
  31.      size_t keysize;
  32.      int fldc;
  33.      const btfield_t fldv[];
  34.  
  35. DESCRIPTION
  36.      The btcreate function creates the btree file named by filename.
  37.      m is the degree of the btree and keysize is the size of the keys.
  38.  
  39.      fldc is the field count.  It specifies the number of fields in
  40.      the keys stored in this btree.  fldv is an array of field
  41.      definition structures.  fldv must have fldc elements.  The field
  42.      definition structure is defined in <btree.h> as type btfield_t.
  43.      It has the following members.
  44.  
  45.           size_t offset;      /* offset of field in key *\/
  46.           size_t len;         /* field length *\/
  47.           int (*cmp)(void *p1, void *p2, size_t n);
  48.                               /* comparison function for field *\/
  49.           int flags;          /* flags *\/
  50.  
  51.      offset and len specify the location and length of the field,
  52.      respectively.  cmp is a pointer to the user written comparison
  53.      function which defines the sort sequence for the field; p1 and p2
  54.      point to the two fields to be compared and n is the field length.
  55.      The return value must be less than, equal to, or greater than
  56.      zero if p1 is less than, equal to, or greater than p2,
  57.      respectively.  btree field flags values are constructed by
  58.      bitwise OR-ing together flags from the following list (one and
  59.      only one of the first two may be used).
  60.  
  61.      BT_FASC        ascending order
  62.      BT_FDSC        descending order
  63.  
  64.      The major sort is on the first field in fldv.  For keys for which
  65.      the first fields are identical (as reported by the comparison
  66.      function for that field), a minor sort is performed using the
  67.      second field, etc.  The physical order of the fields in the key is
  68.      not significant, and fields may overlap.
  69.  
  70.      btopen will fail if one or more of the following is true:
  71.  
  72.      [EEXIST]       The named btree file exists.
  73.      [EINVAL]       filename is the NULL pointer.
  74.      [EINVAL]       m is less than 3.
  75.      [EINVAL]       keysize is less than 1.
  76.      [EINVAL]       fldc is less than 1.
  77.      [EINVAL]       fldv is the NULL pointer.
  78.      [EINVAL]       fldv contains an invalid field definition.
  79.      [BTEMFILE]     Too many open btrees.  The maximum is defined as
  80.                     BTOPEN_MAX in btree.h.
  81.  
  82. SEE ALSO
  83.      btopen.
  84.  
  85. DIAGNOSTICS
  86.      Upon successful completion, a value of 0 is returned.  Otherwise,
  87.      a value of -1 is returned, and errno set to indicate the error.
  88.  
  89. ------------------------------------------------------------------------------*/
  90. #ifdef AC_PROTO
  91. int btcreate(const char *filename, int m, size_t keysize, int fldc, const btfield_t fldv[])
  92. #else
  93. int btcreate(filename, m, keysize, fldc, fldv)
  94. const char *filename;
  95. int m;
  96. size_t keysize;
  97. int fldc;
  98. const btfield_t fldv[];
  99. #endif
  100. {
  101.     btree_t *    btp    = NULL;
  102.     int        terrno    = 0;        /* tmp errno */
  103.  
  104.     /* validate arguments */
  105.     if (filename == NULL || m < 3 || !bt_fvalid(keysize, fldc, fldv)) {
  106.         errno = EINVAL;
  107.         return -1;
  108.     }
  109.  
  110.     /* find free slot in btb table */
  111.     for (btp = btb; btp < (btb + BTOPEN_MAX); ++btp) {
  112.         if (!(btp->flags & BTOPEN)) {
  113.             break;        /* found */
  114.         }
  115.     }
  116.     if (btp >= btb + BTOPEN_MAX) {
  117.         errno = BTEMFILE;    /* no free slots */
  118.         return -1;
  119.     }
  120.  
  121.     /* load btree_t structure */
  122.     btp->bthdr.flh = NIL;
  123.     btp->bthdr.m = m;
  124.     btp->bthdr.keysize = keysize;
  125.     btp->bthdr.flags = 0;
  126.     btp->bthdr.root = NIL;
  127.     btp->bthdr.first = NIL;
  128.     btp->bthdr.last = NIL;
  129.     btp->bthdr.keycnt = 0;
  130.     btp->bthdr.height = 0;
  131.     btp->bp = NULL;
  132.     btp->flags = BTREAD | BTWRITE;
  133.     btp->fldc = 0;                /* fields */
  134.     btp->fldv = NULL;
  135.     btp->cbtpos.node = NIL;            /* cursor */
  136.     btp->cbtpos.key = 0;
  137.     btp->cbtnp = NULL;
  138.     btp->sp = NULL;
  139.  
  140.     /* create file */
  141.     btp->bp = bopen(filename, "c", sizeof(bthdr_t), (size_t)1, (size_t)0);
  142.     if (btp->bp == NULL) {
  143.         if (errno != EEXIST) BTEPRINT;
  144.         terrno = errno;
  145.         memset(btp, 0, sizeof(*btb));
  146.         btp->flags = 0;
  147.         errno = terrno;
  148.         return -1;
  149.     }
  150.  
  151.     /* write header to file */
  152.     if (bputh(btp->bp, &btp->bthdr) == -1) {    /* header */
  153.         BTEPRINT;
  154.         terrno = errno;
  155.         bclose(btp->bp);
  156.         memset(btp, 0, sizeof(*btb));
  157.         btp->flags = 0;
  158.         errno = terrno;
  159.         return -1;
  160.     }
  161.  
  162.     /* close btp */
  163.     if (btclose(btp) == -1) {
  164.         BTEPRINT;
  165.         return -1;
  166.     }
  167.  
  168.     return 0;
  169. }
  170.